//
//  InformationDetailViewController.m
//  Business Club
//
//  Created by 范琦 on 16/4/11.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "InformationDetailViewController.h"
#import "InformationDetailTopViewZ.h"
#import "YTKNetHandle.h"
#import "InformationModel.h"
#import "UIImageView+WebCache.h"
#import "InformationDetailCell.h"

@interface InformationDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) InformationModel *information;
@property (nonatomic, strong) InformationDetailTopViewZ *topView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation InformationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"资讯详情";
    // Do any additional setup after loading the view.
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];

    [self creatRightItems];
    
    
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"InformationDetailTopViewZ" owner:self options:nil] lastObject];
//    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT / 6);
    [self.view addSubview:_topView];
    [self initTableView];
    [self netHandle];
    
    
}

- (void)initTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT - 20) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = _topView;
    _tableView.sectionFooterHeight = 0;
    _tableView.sectionHeaderHeight = 10;
    _tableView.rowHeight = KSCREENHEIGHT / 3;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"InformationDetailCel" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.backgroundColor = [UIColor whiteColor];
    
}

- (void)creatRightItems
{
    // 分享按钮和收藏按钮的定制
    
    UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
    shareButton.frame = CGRectMake(0, 0, 30, 30);
    
    [shareButton setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
    [shareButton addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    
    
       UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc]initWithCustomView:shareButton];
    
    self.navigationItem.rightBarButtonItems = @[barButtonItem1];
    
    
}

// 网络请求
- (void)netHandle
{

    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:[NSString stringWithFormat:@"/Zx/detail.html?id=%@&token=%@", self.myId,[NSUSERDEFAULTS objectForKey:@"gettoken"]] RegisterApiMethod:RequestMethodGet];
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        if ([request.responseBody[@"error"] isEqualToString:@"0"]) {
            
            self.information = [[InformationModel alloc]init];
            [self.information setValuesForKeysWithDictionary:request.responseBody[@"result"]];
            _topView.title.text = _information.name;
            
            NSString *str = [NSString stringWithFormat:@"%@阅 · %@",_information.view_num,_information.create_datetime_str];
            _topView.time.text = str;
            _topView.content.text = _information.content;
            [self.tableView reloadData];
            
            // 重新计算topView高度 更好的适配
            float higth = 0;
            if (_information.name.length != 0) {
                NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:20]};
                 CGRect rect = [_information.name boundingRectWithSize:CGSizeMake(KSCREENWIDTH - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
                higth += rect.size.height + 15;
            }
            if (_information.content.length != 0) {
                NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
                CGRect rect = [_information.content boundingRectWithSize:CGSizeMake(KSCREENWIDTH - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
                higth += rect.size.height;
            }
            _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, higth + 45);
            _tableView.tableHeaderView = _topView;
            
            
        }else{
            //返回错误
        
        }
        
    } failure:^(YTKBaseRequest *request) {
        
    }];
    



}
#warning tableview代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    InformationDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:self.information.imgs[indexPath.section]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.information.imgs.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, 10)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}
// 分享
- (void)share
{
   
}
// 收藏
- (void)down
{
    NSLog(@"收藏");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
